package org.jeecgframework.codegenerate.util;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.core.TemplateClassResolver;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class TemplateConfiguration {
	
 public static Configuration getConfiguration(List<File> files, String encoding, String str) throws IOException {
     Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
     FileTemplateLoader[] fileTemplates = new FileTemplateLoader[files.size()];

     for(int i = 0; i < files.size(); ++i) {
         fileTemplates[i] = new FileTemplateLoader(files.get(i));
     }

     MultiTemplateLoader templateLoader = new MultiTemplateLoader(fileTemplates);
     configuration.setTemplateLoader(templateLoader);
     configuration.setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
     configuration.setNumberFormat("###############");
     configuration.setBooleanFormat("true,false");
     configuration.setDefaultEncoding(encoding);
     return configuration;
 }

 public static List<String> getFiles(String data, String var1) {
     String[] elements = getElements(data, "\\/");
     List<String> var3 = new ArrayList<String>();
     var3.add(var1);
     var3.add(File.separator + var1);
     String var4 = "";
     for(int i = 0; i < elements.length; ++i) {
         var4 = var4 + File.separator + elements[i];
         var3.add(var4 + File.separator + var1);
     }
     return var3;
 }

 public static String[] getElements(String str, String delim) {
     if (str == null) {
         return new String[0];
     }
     StringTokenizer stringTokenizer = new StringTokenizer(str, delim);
     List<String> listElement = new ArrayList<String>();

     while(stringTokenizer.hasMoreElements()) {
         listElement.add(stringTokenizer.nextElement().toString());
     }
     return listElement.toArray(new String[listElement.size()]);
 }

 public static String processTemplate(String templateStr, Map<String, Object> dataMap, Configuration var2) {
     StringWriter stringWriter = new StringWriter();
     try {
         Template template = new Template("templateString...", new StringReader(templateStr), var2);
         template.process(dataMap, stringWriter);
         return stringWriter.toString();
     } catch (Exception e) {
         throw new IllegalStateException("cannot process templateString:" + templateStr + " cause:" + e, e);
     }
 }

 public static void write(Template template, Map<String, Object> map, File file, String charsetName) throws IOException, TemplateException {
     BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charsetName));
     map.put("Format", new SimpleFormat());
     map.put("currentDate", NonceUtils.e());
     template.process(map, bufferedWriter);
     bufferedWriter.close();
 }
}

